<template>
  <el-dialog :close-on-click-modal='false' v-drag :title="$t('添加')" :v-if="dialogVisible" :visible.sync="dialogVisible" :width="lang==='zh' ? '440px':'508px'" @close='close' top="20vh">
    <div class="dialogContentBoxMaxHeight60">
      <el-form ref="form" :model="record" :label-width="lang==='zh' ?'90px':'160px'" :rules="rules" label-position="left" style="margin:0 20px;">
        <el-form-item :label="$t('入站接口名称')" prop="inbound_interface">
          <el-select v-model="record.inbound_interface" ref='inboundInterface' clearable filterable allow-create default-first-option size='small' :placeholder="$t('请选择')" :style="lang==='zh' ? 'width:200px':'width:260px'">
            <el-option v-for="item in interfaceList" :disabled="item.enPortMode === 3" :key="item.strPhysicsName" :label="item.strPhysicsName" :value="item.strPhysicsName">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('IP类型')" prop="ip">
          <el-select v-model="record.ip" ref="recordIp" clearable size='small' :style="lang==='zh' ? 'width:200px':'width:260px'">
            <el-option v-for="item in ipList" :key="item.value" :label="item.lable" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('源地址')" prop="source_ip">
          <el-input v-emoji v-model="record.source_ip" ref='sourceIp' :style="lang==='zh' ? 'width:200px':'width:260px'" size='small'></el-input>
        </el-form-item>
        <el-form-item :label="$t('组播IP')" prop="group_ip">
          <el-input v-emoji v-model="record.group_ip" ref='groupIp' :style="lang==='zh' ? 'width:200px':'width:260px'" size='small'></el-input>
        </el-form-item>
        <el-form-item :label="$t('出站接口名称')" prop="outbound_interface">
          <el-select v-model="record.outbound_interface" ref="outboundInterface" multiple clearable collapse-tags filterable :placeholder="$t('请选择出接口')" @change="changeSelect" size='small' :style="lang==='zh' ? 'width:200px':'width:260px'">
            <div class="selectBox">
              <el-checkbox v-model="checked" :indeterminate="isIndeterminate" class="isIndeterminate" @change='selectAll'>{{$t('全选')}}</el-checkbox>
              <el-link :underline="false" @click='selectRe' class="selectRe" style="line-height: 19px;padding-left:20px;">{{$t('反选')}}</el-link>
            </div>
            <el-option v-for="item in interfaceList" :disabled="item.enPortMode === 3" :key="item.strPhysicsName" :label="item.strPhysicsName" :value="item.strPhysicsName">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit" class="addSubmit">{{$t('确 定')}}</el-button>
      <el-button @click="close" class="addClose">{{$t('取 消')}}</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { addRouteMulticast } from '@/api/network/route/multicastroute'
import { getPhysicalApiArray } from '@/api/network/api/physical'
import { inputClass, selectClass } from '@/utils/input-class.js'
export default {
  name: 'MulticastrouteDialog',
  data () {
    var validateName = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else {
        callback()
      }
    }
    var validateOutboundInterface = (rule, value, callback) => {
      if (value && value.length > 0) {
        callback()
      } else {
        callback(new Error(this.$t('不能为空')))
      }
    }
    var validateIp = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else if (this.record.ip === 'ipv4' && !this.isValidIP(value)) {
        callback(new Error(this.$t('请输入正确的IP地址')))
      } else if (this.record.ip === 'ipv6' && !this.isValidIPv6(value)) {
        callback(new Error(this.$t('请输入正确的IP地址')))
      } else {
        callback()
      }
    }
    var validateSIp = (rule, value, callback) => {
      if (value === '') {
        callback()
      } else if (this.record.ip === 'ipv4' && !this.isValidIP(value)) {
        callback(new Error(this.$t('请输入正确的IP地址')))
      } else if (this.record.ip === 'ipv6' && !this.isValidIPv6(value)) {
        callback(new Error(this.$t('请输入正确的IP地址')))
      } else {
        callback()
      }
    }
    return {
      lang: this.$i18n.locale,
      dialogVisible: false,
      record: {
        inbound_interface: '',
        ip: '',
        source_ip: '',
        group_ip: '',
        outbound_interface: []
      },
      recordCopy: {
        inbound_interface: '',
        ip: '',
        source_ip: '',
        group_ip: '',
        outbound_interface: []
      },
      interfaceList: [],
      interfaceListAll: [],
      checked: false,
      isIndeterminate: false,
      ipList: [
        { lable: 'IPv4', value: 'ipv4' },
        { lable: 'IPv6', value: 'ipv6' }
      ],
      rules: {
        interface: [{ required: false, validator: validateName, trigger: 'blur' }],
        source_ip: [{ required: false, validator: validateSIp, trigger: 'blur' }],
        group_ip: [{ required: false, validator: validateIp, trigger: 'blur' }],
        outbound_interface: [{ required: false, validator: validateOutboundInterface, trigger: 'change' }],
        inbound_interface: [{ required: false, validator: validateOutboundInterface, trigger: 'change' }]
      }
    }
  },
  methods: {
    init (data) {
      this.dialogVisible = true
      this.$nextTick(() => {
        const data = ['outboundInterface', 'sourceIp', 'groupIp']
        const selectData = ['inboundInterface', 'recordIp']
        selectClass(this.$refs, selectData)
        inputClass(this.$refs, data)
      })
      getPhysicalApiArray().then(res => { // strPhysicsName
        if (res.status === 0) {
          this.interfaceList = res.info
          this.interfaceListAll = []
          this.interfaceList.forEach(item => {
            if (item.enPortMode !== 3) {
              this.interfaceListAll.push(item.strPhysicsName)
            }
          })
        }
      })
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          addRouteMulticast(this.record).then(res => {
            if (res.status === 0) {
              this.$message({
                message: this.$t('添加成功'),
                type: 'success'
              })
              this.close()
              this.$parent.listData()
            }
          })
        }
      })
    },
    close () {
      this.dialogVisible = false
      this.record = Object.assign({}, this.recordCopy)
      this.interfaceList = []
      this.checked = false
      this.isIndeterminate = false
      this.$nextTick(() => {
        this.$refs.form.resetFields()
      })
    },
    changeSelect (val) {
      let checkedCount = val.length
      this.checked = checkedCount === this.interfaceListAll.length
      this.isIndeterminate = checkedCount > 0 && checkedCount < this.interfaceListAll.length
    },
    selectAll () {
      this.record.outbound_interface = []
      if (this.checked) {
        this.interfaceList.forEach((item) => {
          if (item.enPortMode !== 3) {
            this.record.outbound_interface.push(item.strPhysicsName)
          }
        })
      } else {
        this.record.outbound_interface = []
      }
      this.isIndeterminate = false
    },
    selectRe () {
      let check_array = []
      for (let item of this.record.outbound_interface) {
        if (item.enPortMode !== 3) {
          check_array.push(item)
        }
      }
      this.record.outbound_interface = this.interfaceListAll.filter((x) => !check_array.some((item) => x === item))
      this.changeSelect(this.record.outbound_interface)
    },
    // 验证ipv4
    isValidIP (ip) {
      let reg = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/
      return reg.test(ip)
    },
    // 验证ipv6
    isValidIPv6 (ip) {
      let reg = /^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/
      // let reg = /^\s*((([0-9A-Fa-f]{1,4}:){7}(([0-9A-Fa-f]{1,4})|:))|(([0-9A-Fa-f]{1,4}:){6}(:|((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})|(:[0-9A-Fa-f]{1,4})))|(([0-9A-Fa-f]{1,4}:){5}((:((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|((:[0-9A-Fa-f]{1,4}){1,2})))|(([0-9A-Fa-f]{1,4}:){4}(:[0-9A-Fa-f]{1,4}){0,1}((:((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|((:[0-9A-Fa-f]{1,4}){1,2})))|(([0-9A-Fa-f]{1,4}:){3}(:[0-9A-Fa-f]{1,4}){0,2}((:((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|((:[0-9A-Fa-f]{1,4}){1,2})))|(([0-9A-Fa-f]{1,4}:){2}(:[0-9A-Fa-f]{1,4}){0,3}((:((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|((:[0-9A-Fa-f]{1,4}){1,2})))|(([0-9A-Fa-f]{1,4}:)(:[0-9A-Fa-f]{1,4}){0,4}((:((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|((:[0-9A-Fa-f]{1,4}){1,2})))|(:(:[0-9A-Fa-f]{1,4}){0,5}((:((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|((:[0-9A-Fa-f]{1,4}){1,2})))|(((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})))(%.+)?\s*$/
      return reg.test(ip)
    }
  }
}
</script>
<style scoped>
.dialogContentBox {
  max-height: calc(60vh - 184px);
  overflow-y: auto;
}

.selectBox {
  padding: 0 20px;
}
</style>
